﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Text;

namespace ZHDJ.Data.Models.Common
{
    public class PartyMessageBoard : EntityBase
    {
        #region 属性
        /// <summary>
        /// 
        /// </summary>
        [StringLength(20)]
        public string UserName { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [StringLength(20)]
        public string MobileNo { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [StringLength(32)]
        public string BranchID { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [StringLength(512)]
        public string Message { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime? CreateTime { get; set; }

        /// <summary>
        /// 创建人
        /// </summary>
        public string CreateBy { get; set; }

        /// <summary>
        /// 修改时间
        /// </summary>
        public DateTime? ModifyTime { get; set; }

        /// <summary>
        /// 修改人
        /// </summary>
        public string ModifyBy { get; set; }


        /// <summary>
        /// 回复人
        /// </summary>
        public string HandleBy { get; set; }

        /// <summary>
        /// 回复时间
        /// </summary>
        public DateTime? HandleTime { get; set; }

        /// <summary>
        /// 回复内容
        /// </summary>
        public string HandleContent { get; set; }

        /// <summary>
        /// 留言状态
        /// </summary>
        public int MessageState { get; set; }


        #endregion

        #region 查询管理
        public static DataSet GetListData(int pageIndex, int pageSize, string searchKey, int MessageState)
        {
            List<MySqlParameter> parameters = new List<MySqlParameter>()
            {
                new MySqlParameter()
                {
                    DbType = DbType.Int32,
                    ParameterName = "Begin",
                    Value = (pageIndex)*pageSize
                },
                new MySqlParameter()
                {
                    DbType = DbType.Int32,
                    ParameterName = "PageSize",
                    Value = pageSize
                }
            };

            StringBuilder sb = new StringBuilder();
            sb.Append(@"select SQL_CALC_FOUND_ROWS
 ID,Message,UserName,MobileNo,CreateTime,MessageState,HandleBy,HandleTime,HandleContent
 from tblpartymessageboard where 1=1");

            if (MessageState == 1)
            {
                sb.Append(" and MessageState = 1");
            }
            if (MessageState == 4)
            {
                sb.Append(" and MessageState = 0");
            }
            if (!string.IsNullOrEmpty(searchKey))
            {
                sb.Append(" and Message like ?Message ");
                parameters.Add(
                    new MySqlParameter()
                    {
                        DbType = DbType.String,
                        ParameterName = "Message",
                        Value = string.Format("%{0}%", searchKey)
                    });
            }

            sb.Append(" order by CreateTime desc LIMIT ?Begin ,?PageSize;SELECT FOUND_ROWS();");
            return MySqlHelper.ExecuteDataSet(sb.ToString(), parameters.ToArray());
        }
        #endregion
    }
}
